package com.sfac.javaSpringBoot.modules.test.service.impl;

import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.common.entity.SearchBean;
import com.sfac.javaSpringBoot.modules.test.dao.StudentRepository;
import com.sfac.javaSpringBoot.modules.test.entity.Student;
import com.sfac.javaSpringBoot.modules.test.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Collections;
import java.util.List;
import java.util.Optional;


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository;

    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public Result<Student> insertStudent(Student student) {
        studentRepository.saveAndFlush(student);
        return new Result<Student>(Result.ResultCode.SUCCESS.code, "Insert success ", student);
    }

    @Override
    public Result<Student> updateStudent(Student student) {
        studentRepository.saveAndFlush(student);
        return new Result<Student>(Result.ResultCode.SUCCESS.code, "Update success ", student);
    }

    @Override
    public Result<Object> deleteStudentById(int id) {
        studentRepository.deleteById(id);
        return new Result<Object>(Result.ResultCode.SUCCESS.code, "Delete success ");
    }

    @Override
    public Student getStudentById(int id) {
        return studentRepository.getStudnetById(id);
    }

    @Override
    public List<Student> getStudents() {
        return Optional.ofNullable(studentRepository.findAll())
                .orElse(Collections.emptyList());
    }

    @Override
    public Student getStudentByNameAndEmail(String name, String email) {
        return studentRepository.findFirstByNameAndEmail(name, email);
    }

    @Override
    public Page<Student> getStudentsBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();
        //
        Sort.Direction direction = StringUtils.isBlank(searchBean.getDirection()) ||
                searchBean.getDirection().toLowerCase().equals("asc") ?
                Sort.Direction.ASC : Sort.Direction.DESC;
        Sort sort = Sort.by(direction, searchBean.getOrderBy());
        Student student = new Student();
        student.setName(searchBean.getKeyword());
        student.setEmail(searchBean.getKeyword());
        ExampleMatcher exampleMatcher = ExampleMatcher
                .matchingAny()
                .withMatcher("name", match -> match.contains())
                .withMatcher("email", match -> match.contains())
                ;

        Pageable pageable = PageRequest
                .of(searchBean.getCurrentPage() - 1,
                searchBean.getPageSize(),
                sort);
        Example example = Example.of(student, exampleMatcher);
        return studentRepository.findAll(example, pageable);
    }
}
